/*
 * DrawedObjectXML.java
 *
 * Created on 18 sierpień 2007, 16:28
 *
 */

package pl.krakow.ae.knp.wikiboard.xml;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import nu.xom.*;

/**
 *
 * @author Michał
 */
public class DrawedObjectXML implements Serializable {
    
    private int id;
    private String drawableType;
    private List<PointXML> points;
    private DimensionsXML dimensions;
    private int[] colors;
    private String text;
    
    /**
     * Creates a new instance of DrawedObjectXML
     */
    public DrawedObjectXML(int id, String drawableType, List<PointXML> points,
            DimensionsXML dimensions, int[] colors, String text) {
        this.id = id;
        this.drawableType = drawableType;
        this.points = points;
        this.dimensions = dimensions;
        this.colors = colors;
        this.text = text;
    }
    
    public Element getXML() {
        Element drawedObject = new Element("drawedObject");
        
        Element eId = new Element("id");
        eId.appendChild(String.valueOf(id));
        drawedObject.appendChild(eId);
        
        Element eDrawableType = new Element("drawableType");
        eDrawableType.appendChild(drawableType);
        drawedObject.appendChild(eDrawableType);        
        
        for (PointXML p : points) {
            Element ePoint = p.getXML();
            drawedObject.appendChild(ePoint);            
        }       
        
        Element eDimensions = dimensions.getXML();
        drawedObject.appendChild(eDimensions);
        
        for (int c : colors) {
            Element eColor = new Element("color");
            eColor.appendChild(String.valueOf(c));
            drawedObject.appendChild(eColor);            
        }
        
        Element eText = new Element("text");            
        eText.appendChild(text);
        drawedObject.appendChild(eText);
        
        
        return drawedObject;
    }
    
    public static void main(String[] args) {
        
        PointXML p1 = new PointXML("1","2");
        PointXML p2 = new PointXML("3","4");
        PointXML p3 = new PointXML("5","6");
        ArrayList points = new ArrayList();
        points.add(p1);
        points.add(p2);
        points.add(p3);
        int[] colors = {8,9};
        
        DimensionsXML d = new DimensionsXML(6,7);
        
        DrawedObjectXML dox = new DrawedObjectXML(1,"Ellipse",points,d,colors,"tekst");
        
        Document doc = new Document(dox.getXML());
        try {
            Serializer serializer = new Serializer(new FileOutputStream("c:\\dox.xml"));
            serializer.setIndent(4);
            serializer.write(doc);
        } catch(IOException ex) {
            System.err.println(ex);
        }
    }
}
